package com.ikas.ai.server.module.risk.service;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ikas.ai.server.error.BusinessException;
import com.ikas.ai.server.module.risk.dao.RiskPolicyDAO;
import com.ikas.ai.server.module.risk.model.RiskPolicy;
import com.ikas.ai.utils.CollectionUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 风险处置策略服务类
 *
 * @author tang.xuandong
 * @version 1.0.0
 * @date 2023/1/31 13:57
 */
@Service
public class RiskPolicyService extends ServiceImpl<RiskPolicyDAO, RiskPolicy> {

    @Autowired
    private RiskPolicyDAO riskPolicyDAO;

    /**
     * 分页查询
     *
     * @param machineNo 风险名称
     * @param systemNo  系统编码
     * @return list
     */
    public List<RiskPolicy> pageQueryRiskPolicy(String riskName, String systemNo, String machineNo) {
        LambdaQueryChainWrapper<RiskPolicy> lambdaQueryChainWrapper = new LambdaQueryChainWrapper<>(riskPolicyDAO);
        return lambdaQueryChainWrapper
                .like(StringUtils.isNotBlank(riskName), RiskPolicy::getName, riskName)
                .eq(StringUtils.isNotBlank(systemNo), RiskPolicy::getSystemNo, systemNo)
                .eq(StringUtils.isNotBlank(machineNo), RiskPolicy::getMachineNo, machineNo)
                .list();
    }

    /**
     * 查询风险处置策略，通过系统编码
     *
     * @param systemNo 统编码
     * @return List<RiskPolicy>
     */
    public List<RiskPolicy> getRiskPolicyBySystemNo(String systemNo) {
        LambdaQueryChainWrapper<RiskPolicy> lambdaQueryChainWrapper = new LambdaQueryChainWrapper<>(riskPolicyDAO);
        return lambdaQueryChainWrapper
                .eq(StringUtils.isNotBlank(systemNo), RiskPolicy::getSystemNo, systemNo)
                .list();
    }

    /**
     * 查询风险处置策略，通过ids
     *
     * @param ids ids
     * @return List<RiskPolicy>
     */
    public List<RiskPolicy> getRiskPolicyByIds(List<Long> ids) {
        if (CollectionUtil.isEmpty(ids)) {
            return new ArrayList<>();
        }
        LambdaQueryChainWrapper<RiskPolicy> lambdaQueryChainWrapper = new LambdaQueryChainWrapper<>(riskPolicyDAO);
        return lambdaQueryChainWrapper
                .in(!CollectionUtil.isEmpty(ids), RiskPolicy::getId, ids)
                .list();
    }

    /**
     * 新增风险策略
     *
     * @param param RiskPolicy
     */
    public int saveRiskPolicy(RiskPolicy param) {
        String name = param.getName();
        String systemNo = param.getSystemNo();
        if (StringUtils.isBlank(name) || StringUtils.isBlank(systemNo)) {
            throw new BusinessException("风险名称或系统编号不能为空");
        }
        param.setCreateTime(new Date());
        return riskPolicyDAO.insert(param);
    }

    /**
     * 编辑风险策略
     *
     * @param param RiskPolicy
     * @return row
     */
    public int updateRiskPolicy(RiskPolicy param) {
        Long id = param.getId();
        String name = param.getName();
        String systemNo = param.getSystemNo();
        if (id == null) {
            throw new BusinessException("ID不能为空");
        }
        if (StringUtils.isBlank(name) || StringUtils.isBlank(systemNo)) {
            throw new BusinessException("风险名称或系统编号不能为空");
        }
        param.setUpdateTime(new Date());
        return riskPolicyDAO.updateById(param);
    }

    /**
     * 删除风险策略
     *
     * @param ids list
     */
    public int deleteRiskPolicy(List<Long> ids) {
        return riskPolicyDAO.deleteBatchIds(ids);
    }

}
